import originJsonp from 'jsonp'

export default function jsonp2 (url, data, option) {
  url += (url.indexOf('?') < 0 ? '?' : '&') + params(data)
  return new Promise((resolve, reject) => {
    originJsonp(url, option, (err, data) => {
      !err ? resolve(data) : reject(err)
    })
  })
}

function params (data) {
  let url = ``
  for (let k in data) {
    let value = data[k] !== undefined ? data[k] : ''
    url += `&${k}=${encodeURIComponent(value)}`
  }
  return url ? url.substring(1) : ''
}
